package com.hotlove.usercenter.service.impl;

import com.hotlove.usercenter.domain.Profile;
import com.hotlove.usercenter.dto.Crendential;
import com.hotlove.usercenter.service.CredentialService;
import com.hotlove.usercenter.service.strategy.SessionStrategy;
import com.hotlove.usercenter.service.strategy.StrategyContant;
import com.hotlove.usercenter.service.token.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: hotlove_linx
 * @Date: 2022/4/17 16:57
 * @Description:
 */
@Service
public class CredentialServiceImpl implements CredentialService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "uuidTokenService")
    private TokenService tokenService;

    @Resource(name = StrategyContant.KICKOUTLAST_STRATEGY)
    private SessionStrategy sessionStrategy;

    /**
     * 构建credential
     * 1.登录成功后获取用户信息传入进行构建
     * 2.构建credential对象，绑定终端，用户id, token信息 及基础信息
     * 3.将构建好得credential信息缓存到redis作为session
     * 4.返回credential
     * @param profile 用户信息
     * @param terminal 登录终端类型
     * @return
     */
    @Override
    public Crendential createCredential(Integer terminal, Profile profile) {

        // 创建登录凭证
        Crendential crendential = new Crendential.Builder()
                .token(tokenService.createToken())
                .id(profile.getId())
                .name(profile.getName())
                .address(profile.getAddress())
                .email(profile.getEmail())
                .sex(profile.getSex())
                .phone(profile.getPhone())
                .terminal(terminal)
                .build();

        // 创建session到redis 这里需要修改究竟使用那种配置改为配置类型
        sessionStrategy.createSession(crendential);

        return crendential;
    }
}
